// pages/shoplist/shoplist.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filterState:false,
    params:{
      issales:0,
      isprice:0,
      p:1
    },
    minprice:'',
    maxprice:'',
    paramState:0,
    shoplist:[],
    brandlist:[],
    list_layout:false
  },
  change_filterState:function(){
    this.setData({
      filterState:!this.data.filterState,
    })
  },
  getShoplist:function(params){
    console.log(params)
    let data=Object.assign(this.data.params,params);
    app.requestJs('Goods/goodsList',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        this.setData({
          shoplist:data.p==1?resData:[...this.data.shoplist,...resData],
          params:data,
          pageloadding:resData.length==10?false:true,
          filterState:false
        })
      }else app.utilJs.toast({ title: res.message });
    })
  },
  change_default:function(){
    this.setData({
      paramState:0
    })
    this.getShoplist({issales:0,isprice:0,p:1});
  },
  change_issales:function(){
    let issales=this.data.params.issales;;
    if(this.data.params.issales==0)issales=1;
    else issales=issales==1?2:1;
    this.setData({
      paramState:1
    })
    this.getShoplist({issales,isprice:0,p:1});
  },
  change_isprice:function(){
    let isprice=this.data.params.isprice;
    if(this.data.params.isprice==0)isprice=1;
    else isprice=isprice==1?2:1;
    this.setData({
      paramState:2
    })
    this.getShoplist({issales:0,isprice,p:1});
  },
  confirm_filter:function(){
    let minprice=this.data.minprice;
    let maxprice=this.data.maxprice;
    if(maxprice.length=='')this.getShoplist({minprice,maxprice,p:1});
    else if(Number(minprice)>Number(maxprice)) {
      app.utilJs.toast({ title:"最低价必须低于最高价"});
    }
    else this.getShoplist({minprice,maxprice,p:1});
  },
  reset_filter:function(){
    this.setData({
      minprice:'',
      maxprice:''
    })
    this.getShoplist({minprice:'',maxprice:'',bid:'',p:1});
  },
  bindbrand:function(e){
    let bid=e.currentTarget.dataset.bid;
    if(bid==this.data.params.bid)bid='';
    this.getShoplist({p:1,bid});
  },
  getbrandlist:function(){
    let data={};
    app.requestJs('Goods/brandlist',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        this.setData({
          brandlist:resData
        })
      }else app.utilJs.toast({ title: res.message });
    })
  },
  bindminprice:function(e){
    let value=e.detail.value,minprice=this.data.minprice,maxprice=this.data.maxprice;
    // if(maxprice.length>0)value=value>maxprice?minprice:value;
    this.setData({
      minprice:value
    })
  },
  bindmaxprice:function(e){
    let value=e.detail.value,minprice=this.data.minprice,maxprice=this.data.maxprice;
    // if(minprice.length>0)value=value<=minprice?maxprice:value;
    this.setData({
      maxprice:value
    })
  },
  getsizelist:function(){
    let data={goods_id};
    app.requestJs('goods/getgoodsitem',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        this.setData({
          sizelist:resData
        })
      }else app.utilJs.toast({ title: res.message });
    })
  },
  bindLayout:function(){
    this.setData({
      list_layout:!this.data.list_layout
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShoplist(options);
    this.getbrandlist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底");
    if(this.data.pageloadding)return;
    let p=this.data.params.p+1;
    this.getShoplist({p});
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})